import O1HD from "../../../engines/gameEngine";

export interface DrawColor {
    r: number;
    g: number;
    b: number;
}

export interface DrawDataNode {
    isStart: boolean;
    lineWidth: number;
    color: DrawColor;
    x: number;
    y: number;
}

export interface DrawInfoData {
    name: string;
    drawList: DrawDataNode[];
}

export class DrawBase {
    [name: string]: DrawInfoData
}

export default class o1DrawInterface {
    private readonly m_BASE_DATA_KEY: string = 'o1-draw-data';
    private m_baseInfo: DrawBase = null;

    //
    constructor() {
    }

    getData(name: string) {
        return this.m_baseInfo[name];
    }

    //
    OnLoadData() {
        this.m_baseInfo = new DrawBase();
        let data = O1HD.getServices().localStorageSrv.getStorage(this.m_BASE_DATA_KEY);
        if (data) {
            let result: DrawInfoData[] = JSON.parse(data);
            for (let info of result) {
                this.m_baseInfo[info.name] = info;
            }
        }
    }

    AddAndWriteDrawData(data: DrawInfoData) {
        //已经存在
        this.m_baseInfo[data.name] = data;
        this.WriteData();
    }

    DeleteData(name: string) {
        this.m_baseInfo[name] && delete this.m_baseInfo[name];
        this.WriteData();
    }

    getAllData() {
        return this.m_baseInfo;
    }

    private WriteData() {
        //
        let ary = [];
        for (let key in this.m_baseInfo) {
            let info = this.m_baseInfo[key];
            ary.push(info);
        }
        O1HD.getServices().localStorageSrv.setStorage(this.m_BASE_DATA_KEY, JSON.stringify(ary));
    }

    //
}